<template>
    <el-dialog title="选择技术要求" v-model="dialogOpen">

        <el-scrollbar height="800">
            <el-table v-loading="loading" :data="technologyRequireList" row-key="id" default-expand-all
                :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" style="height: 680px;"
                @selection-change="handleSelectionChange" ref="tableRef">
                <el-table-column type="selection" width="55" align="center" :selectable="selectable" />
                <!-- :selectable="selectable"  -->
                <el-table-column label="内容" align="left" prop="content" />
                <el-table-column label="创建时间" align="center" prop="createTime" width="200">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
            </el-table>

        </el-scrollbar>


        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitData">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </template>

    </el-dialog>
</template>

<script setup name="TechnologyRequireCom">
import { listTechnologyRequire, listAllTechnologyRequire, getTechnologyRequire, delTechnologyRequire, addTechnologyRequire, updateTechnologyRequire } from "@/api/flow/technologyRequire";

const { proxy } = getCurrentInstance();

const props = defineProps({
    TRCDialog: {
        type: Boolean,
        default: false
    },
    TRCText: {
        type: Array,
        default: []
    }
});

const emits = defineEmits(['update:TRCDialog', 'update:TRCText']);

const dialogOpen = ref(props.TRCDialog)
const technologyRequireList = ref([]);
const noTreeDataList = ref([])
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const refreshTable = ref(true);
const deptOptions = ref([]);
const isExpandAll = ref(true);
const checkedRows = ref([]);
const tableRef = ref(null)
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        content: null
    },
    rules: {
        content: [{ required: true, message: "内容不能为空", trigger: "blur" }]
    }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询技术要求列表 */
function getList() {
    loading.value = true;
    listAllTechnologyRequire(queryParams.value).then(response => {
        noTreeDataList.value = reset.data;
        technologyRequireList.value = proxy.handleTree(response.data, "id");
        loading.value = false;
        // 数据加载完成后进行默认勾选操作
        if (props.TRCText.length > 0) {
            nextTick(() => {
                let selectionIds = props.TRCText.map(item => item.id);
                let findList = response.data.filter(item => selectionIds.includes(item.id));
                findList.forEach(item => {
                    if (tableRef.value) {
                        tableRef.value.toggleRowSelection(item, true);
                    }
                });
            });
        }
    });
}

const handleSelectionChange = (rows) => {
    checkedRows.value = rows;
};

const selectable = (row) => {
    if (row.level === '2') {
        return true;
    } else {
        return false;
    }
};

function submitData() {
    emits('update:TRCDialog', false);
    emits('update:TRCText', checkedRows.value);
}

function cancel() {
    emits('update:TRCDialog', false);
}

// 表单重置
function reset() {
    form.value = {
        id: null,
        type: null,
        content: null
    };
    proxy.resetForm("technologyRequireRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}



/** 展开/折叠操作 */
function toggleExpandAll() {
    refreshTable.value = false;
    isExpandAll.value = !isExpandAll.value;
    nextTick(() => {
        refreshTable.value = true;
    });
}

/** 新增按钮操作 */
function handleAdd(row) {
    reset();
    listAllTechnologyRequire(queryParams.value).then(response => {
        deptOptions.value = proxy.handleTree(response.data, "id");
    });
    if (row != undefined) {
        form.value.parentId = row.id;
    }
    open.value = true;
    title.value = "添加技术要求";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    listAllTechnologyRequire(queryParams.value).then(response => {
        deptOptions.value = proxy.handleTree(response.data, "id");
    });
    const _id = row.id || ids.value
    getTechnologyRequire(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改技术要求";
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除技术要求编号为"' + _ids + '"的数据项？').then(function () {
        return delTechnologyRequire(_ids);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}


getList();
</script>